#pragma once

#include <iostream>
#include <string>
#include <string.h>
#include <pthread.h>

#include "TcpServer.hpp"
#include "HttpProtocol.hpp"
#include "MyPthread/Log.hpp"

namespace HttpServerModule
{
    using namespace LogModule;
    using namespace TcpServerModule;

    const std::string Sep = "\r\n";
    const std::string LineSep = " ";
    const std::string HeaderLineSep = ": ";
    const std::string BlankLine = Sep;

    class HttpServer
    {
    private:
        bool SafeCheck(const std::string &service)
        {
            return _router.find(service)!= _router.end();
        }
        bool HandlerHttpRequest(SockPtr sockfd, InetAddr client)
        {
            std::string http_request;
            sockfd->Recv(&http_request);
            std::cout << "http_request:" << http_request << std::endl;
            // std::cout << http_request << std::endl; // 字节流的请求
            //  读取请求，对请求进行分析处理 --> 文本处理！
            //  1. 读取请求的完整性 --- 暂时不做
            //  2. 完整http反序列化，http response序列化...

            HttpRequest request;
            request.Deserialize(http_request);
            HttpResponse resp;

            if (request.IsHasArgs())
            {
                std::cout << "-----IsHasArgs()" << std::endl;
                // 2. 提交参数，携带参数，需要我们进行交互设置
                std::string service = request.Path();
                if (SafeCheck(service))
                    _router[request.Path()](request, resp); // /login
                else
                    resp.Build(request); // debug
            }
            else
            {
                resp.Build(request);
            }

            

            // //  demo 1 : 直接返回一个固定的内容
            // std::string status_line = "HTTP/1.1 200 OK" + Sep + BlankLine;
            // // 直接返回一个html网页
            // std::string body = "";
            // std::string httpresponse = status_line + body;

            std::string httpresponse;
            resp.Serialize(&httpresponse);
            LOG(LogLevel::DEBUG) << "------服务器响应: ";
            std::cout << "httpresponse:" << httpresponse << std::endl;
            sockfd->Send(httpresponse);
            return true;
        }

    public:
        HttpServer(int port)
        {
            // LOG(LogLevel::DEBUG)<<"HttpServer::HttpServer(int port)";
            _tsvr = std::make_unique<TcpServer>(port);
            _tsvr->RegisterSerive([this](SockPtr sockptr, InetAddr client)
                                  { return HandlerHttpRequest(sockptr, client); });
        }

        void Start()
        {
            LOG(LogLevel::DEBUG) << "HttpServer::Start()";
            _tsvr->Loop();
        }
        void RegisterService(const std::string &service,
                std::function<void(HttpRequest &, HttpResponse &)> handler)
        {
            _router[service] = handler;
        }
        ~HttpServer() {}

    private:
        std::unique_ptr<TcpServer> _tsvr;
        std::unordered_map<std::string, std::function<void(HttpRequest &, HttpResponse &)>> _router;
    };

}